package com.glory.good_dark.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.good_dark.R;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.LotInfoEntity;
import com.glory.good_dark.entity.MergeEntity;
import com.glory.good_dark.entity.StrIDIPEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;

import org.w3c.dom.Text;

import java.util.List;

/**
 * FileName: MergeLotAdapter
 * Author: XJH
 * Date: 2021/7/15 17:38
 * Description:
 * History:
 */
public class MergeLotAdapter extends BaseQuickAdapter<MergeEntity, BaseViewHolder> {
    private FragmentManager fragmentManager;
    private FreeUI_GeneralFragmentDialog dialogs;
    private FreeUI_GeneralFragmentDialog confirmDialog;

    public MergeLotAdapter(@Nullable @org.jetbrains.annotations.Nullable List<MergeEntity> data,FragmentManager fragmentManager) {
        super(R.layout.adapter_merge_lot,data);
        this.fragmentManager = fragmentManager;
    }

    @Override
    protected void convert(BaseViewHolder helper, MergeEntity item) {
        helper.setText(R.id.tvLot,item.getSUB_LOTID()).setText(R.id.tvQTY,item.getQTY());
        helper.getView(R.id.tvLot).setOnClickListener(v -> {
            dialogs = FreeApi_DialogUtils.getScanDialog(mContext, "请输入批次号", s -> {
                helper.setText(R.id.tvLot,s);
                item.setSUB_LOTID(s);
//                getQTY(s,helper.getView(R.id.tvQTY),item);
                dialogs.dismiss();
            });
            dialogs.show(fragmentManager, "SCAN_DIALOG");
        });

        helper.getView(R.id.btnDelete).setOnClickListener(v -> {
            confirmDialog = FreeApi_DialogUtils.getConfirmAndCancelDialog(mContext, "确认删除当前批次？", () -> {
                remove(helper.getLayoutPosition());
                confirmDialog.dismiss();
            }, () -> confirmDialog.dismiss());
            confirmDialog.show(fragmentManager,"CONFIRM_DIALOG");

        });

        EditText etQty = helper.getView(R.id.tvQTY);
        setEditListener(etQty,item);

    }

    private void getQTY(String s, TextView tv,MergeEntity entity) {
        FreeUI_EntityCallBack<BaseEntity<LotInfoEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<LotInfoEntity>>
                (new TypeToken<BaseEntity<LotInfoEntity>>() {
                }.getType(), fragmentManager, this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<LotInfoEntity>> response) {
                if (response.body().isSuccess(mContext, fragmentManager)) {
                    LotInfoEntity lotInfoEntity = response.body().getData().getBody();
                    tv.setText(String.valueOf(lotInfoEntity.getINPUT_QTY()));
                    entity.setQTY(String.valueOf(lotInfoEntity.getINPUT_QTY()));
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<LotInfoEntity>> response) {
                super.onError(response);
            }
        };

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID", App.EQPID);
        jsonObject.addProperty("LOT_ID", s);
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAEQPInfo", jsonObject);
    }

    private void setEditListener(EditText editText, MergeEntity item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setQTY(s.toString());
            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
